package com.cavasmonitor.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;

import com.cavasmonitor.db.CavasDao;

public class Utils {

	public static List<String> getPuntos(Context ctx) {

		String clave = "<td><span style=\"color:green; font-weight:bold;\">";
		List<String> puntos = new ArrayList<String>();

		try {

			CavasDao dao = new CavasDao(ctx);

			dao.open();
			URL url = new URL("http://" + dao.getValueConfig("IP") + "/");
			dao.close();

			URLConnection uc = url.openConnection();
			uc.connect();
			BufferedReader in = new BufferedReader(new InputStreamReader(
					uc.getInputStream()));
			String inputLine;
			while ((inputLine = in.readLine()) != null) {
				if (inputLine.contains(clave)) {
					inputLine = inputLine.replaceAll("\\<", "");
					inputLine = inputLine.replaceAll("\\>", "");
					inputLine = inputLine.replaceAll("\\=", "");
					inputLine = inputLine.replaceAll("\\:", "");
					inputLine = inputLine.replaceAll("\\;", "");
					inputLine = inputLine.replaceAll("\\\"", "");
					inputLine = inputLine.replaceAll("\\/", "");
					inputLine = inputLine.replaceAll("-[a-z]", "");
					inputLine = inputLine.replaceAll("[a-z]", "");
					inputLine = inputLine.replaceAll(" ", "");
					puntos.add(inputLine);
				}
			}
			in.close();
		} catch (Exception ex) {
			Log.e("getPuntos", ex.getCause().toString());
		}
		return puntos;
	}

	@SuppressLint("SimpleDateFormat")
	public static String dateToString(Date date) {

		SimpleDateFormat sdf;
		String fecha = "";

		try {

			sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
			fecha = sdf.format(date);

		} catch (Exception ex) {
			Log.e("dateToString", ex.getCause().toString());
		}
		return fecha;
	}
}
